﻿using System;
using Framework.MVC;
using Identity.Model;
using Account.Model;
using Account.Model.Behavior;
using Identity.IService;
using Identity.Service;
using Framework.MVVM.Model;
using Account.IService;
using Account.Service;



namespace FrontCenter.Models
{
    [ServiceAttribute(typeof(IMobileService), typeof(MobileService), "Instance")]
    [ServiceAttribute(typeof(IIdentityService), typeof(IdentityService), "Instance")]
    public class GetMobileModel : AsyncModelBase, IValidateIdentity, IGetMobile
    {
        #region Field

        private const string m_resource_user = "u000";
        private const string m_resource = "user";
        private const string m_error = "error";

        #endregion Field

        #region AsyncModelBase

        protected override void OnInit(Type type)
        {
            OnBeginRequest();
            this.ValidateIdentity(IdentitySource!=null?IdentitySource:m_resource_user, string.Empty, string.Empty, string.Empty);
        }

        #endregion AsyncModelBase

        #region IValidateIdentity

        public string IdentityCode { get; set; }

        public string IdentitySource { get; set; }

        public bool IsOnce { get; set; }

        public string Source { get; set; }

        public void OnValidateIdentityComplete(string key, Identity.IService.Entry.IdentityEntry entry)
        {
            AccountCode = entry.value;
            this.GetMobile(m_resource,string.Empty,string.Empty);
        }

        public void OnValidateIdentityError(string key)
        {
            OnComplete(m_error, "identity");
            OnEndRequest();
        }

        #endregion IValidateIdentity

        #region IGetMobile

        public string AccountCode { get; set; }

        public void OnGetMobileComplete(string key, Account.IService.Entry.ValueEntity<string> entry)
        {
            OnComplete("mobile", entry);
            OnEndRequest();
        }

        public void OnGetMobileError(string key)
        {
            OnComplete(m_error, "mobile");
            OnEndRequest();
        }

        #endregion IGetMobile

    }
}